package com.xposed.aliproxy.crash;

import android.content.Context;

import com.xposed.aliproxy.utils.DateUtil;
import com.xposed.aliproxy.utils.MyLog;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;

public class CrashHandler implements UncaughtExceptionHandler {
    private static CrashHandler mInstance;
    private static BufferedWriter bufferedWriter;
    private UncaughtExceptionHandler exceptionHandler;
    private Context context;

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        if (mInstance == null) {
            mInstance = new CrashHandler();
        }
        return mInstance;
    }

    public void init(Context context) {
        this.context = context;
        this.exceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    public void uncaughtException(Thread thread, Throwable th) {
        writeCrash(th);
        this.exceptionHandler.uncaughtException(thread, th);
    }

    private void writeCrash(Throwable th) {
        StringBuffer stringBuffer = new StringBuffer();
        Writer stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        th.printStackTrace(printWriter);
        for (th = th.getCause(); th != null; th = th.getCause()) {
            th.printStackTrace(printWriter);
        }
        printWriter.close();
        stringBuffer.append(stringWriter.toString());
        try {
            File file = new File("/sdcard/keyinputtool/crash/");
            if (!file.exists()) {
                file.mkdirs();
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("crash_");
            stringBuilder.append(DateUtil.formatData("yyyyMMdd"));
            stringBuilder.append(".txt");
            String stringBuilder2 = stringBuilder.toString();
            StringBuilder stringBuilder3 = new StringBuilder();
            stringBuilder3.append("/sdcard/keyinputtool/crash/");
            stringBuilder3.append(stringBuilder2);
            File file2 = new File(stringBuilder3.toString());
            if (!file2.exists()) {
                file2.createNewFile();
            }
            if (bufferedWriter == null) {
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));
            }
            stringBuilder = new StringBuilder();
            stringBuilder.append(DateUtil.formatData("yyyy-MM-dd HH:mm:ss:SSS"));
            stringBuilder.append("\n");
            stringBuilder2 = stringBuilder.toString();
            stringBuilder3 = new StringBuilder();
            stringBuilder3.append(stringBuilder2);
            stringBuilder3.append(stringBuffer.toString());
            stringBuilder3.append("\n");
            bufferedWriter.append(stringBuilder3.toString());
            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
            MyLog.printLog("crash", e.getLocalizedMessage());
        }
    }
}
